<?php
define('ROOT_DIR',dirname(__FILE__));
define('DATA_DIR',ROOT_DIR.'/data');
define('SHARE_DIR',ROOT_DIR.'/share');
define('TMP_DIR',ROOT_DIR.'/tmp');
require_once('default-conf.php');

if (file_exists(ROOT_DIR.'/lib')) {
    ini_set('include_path',ini_get('include_path').':'.ROOT_DIR.'/lib');
}

require_once 'dict.php';
check_installation();

if (file_exists(DATA_DIR.'/conf.php')) {
    require_once(DATA_DIR.'/conf.php');
}

require 'view/STE.php';

/*
list(1,2)
    rand
    order by time
*/
$dict_login=false;
if (isset($_POST['password']) && digestPassword($_POST['password'])==$dict_configs['password']) {
    setcookie('password',$dict_configs['password'],time()+3600*24*90);
    $dict_login=true;
} elseif (@$_COOKIE['password']==$dict_configs['password']) {
    $dict_login=true;
}

if (isset($_POST['action']) && $dict_login) {
    $act=$_POST['action'];
    if ($act=='new') {
        $wl = new WordList('new');
        $wl->add($_POST['word']);
        $wl = new WordList('done');
        $wl->delete($_POST['word']);
    } elseif ($act=='done') {
        $wl = new WordList('new');
        $wl->delete($_POST['word']);
        $wl = new WordList('done');
        $wl->add($_POST['word']);
    } elseif ($act=='logout') {
        $dict_login=false;
        setcookie('password',1,time()-1);
    } elseif ($act=='config') {
        if (isset($_POST['new_password'])) {
            $np=trim($_POST['new_password']);
            if ($np) {
                $dict_configs['password'] = digestPassword($np);
            }
        }
        if (isset($_POST['primary_driver']) && in_array($_POST['primary_driver'],$dict_drivers)) {
            $dict_configs['primary_driver'] = $_POST['primary_driver'];
        }
        if (isset($_POST['list_num'])) {
            $dict_configs['list_num'] = (int)$_POST['list_num'];
        }
        if (isset($_POST['disable_drivers']) && is_array($_POST['disable_drivers'])) {
            $dict_configs['disable_drivers'] = array();
            foreach ($_POST['disable_drivers'] as $dd) {
                if (!in_array($dd,$dict_drivers)) continue;
                $dict_configs['disable_drivers'][] = $dd;
            }
        }
        file_put_contents(DATA_DIR.'/conf.php','<?php  $dict_configs='.var_export($dict_configs,true).';');
    }
}
$tpl=new STE();
$tpl->setDir(ROOT_DIR.'/tpl');
$tpl->assign('login',$dict_login);
if (isset($_GET['w'])) {
    $w=trim($_GET['w']);

    /*recent lookup*/
    $_COOKIE['wl']=explode("\n",trim(@$_COOKIE['wl']));
    array_unshift($_COOKIE['wl'],$w);
    $_COOKIE['wl']=array_unique($_COOKIE['wl']);
    if (count($_COOKIE['wl'])>$dict_configs['list_num']) {
        array_pop($_COOKIE['wl']);
    }
    setcookie('wl',implode("\n",$_COOKIE['wl']),time()+3600*24*365*2);

    $tried_drivers = array();
    array_unshift($dict_drivers,$dict_configs['primary_driver']);
    foreach ($dict_drivers as $dict_driver) {
        if (in_array($dict_driver,$dict_configs['disable_drivers']) ||
            in_array($dict_driver,$tried_drivers)
        ) {
            continue;
        }
        $tried_drivers[]=$dict_driver;

        $cache=get_dict_cache();
        $cache_key=$dict_driver.'_'.md5($w);
        if ($detail=$cache->load($cache_key)) {
            break;
        } else {
            require_once 'cidian.php';
            if ($dict_driver=='StarDict') {
                $dict=Cidian::factory($dict_driver,SHARE_DIR.'/stardict');
            } else {
                $dict=Cidian::factory($dict_driver);
            }
            $detail=$dict->lookup($w);
            if ($detail['define']) {
                $cache->save($detail,$cache_key);
                break;
            }
        }
    }
    array_shift($dict_drivers);

    $tpl->assign('word',$w);
    $tpl->assign('define',$detail['define']);
    $tpl->assign('examples',$detail['examples']);
    $tpl->assign('pronounce',$detail['pronounce']);
    if ($dict_login) {
        $wl = new WordList('new');
        $e=$wl->exist($_GET['w']);
        $tpl->assign('existed',$e);
    }
    $tpl->render('word');
} elseif (@$_GET['list']=='done') {
    if ($dict_login) {
        $page=isset($_GET['page'])?(int)$_GET['page']:1;
        $wl = new WordList('done');
        $tpl->assign('words',$wl->get($dict_configs['list_num'],($page-1)*$dict_configs['list_num']));
        $tpl->assign('page',$page);
        $tpl->assign('num',$dict_configs['list_num']);
        $tpl->assign('count',$wl->count());
    }
    $tpl->assign('lt','done');
    $tpl->render('index');
} elseif (@$_GET['list']=='recently') {
    if (isset($_COOKIE['wl'])) {
        $_COOKIE['wl']=explode("\n",@$_COOKIE['wl']);
        $tpl->assign('words',$_COOKIE['wl']);
    } else {
        $tpl->assign('words',array());
    }
    $tpl->assign('lt','recently');
    $tpl->render('index');
} elseif (isset($_GET['admin'])) {
    $tpl->assign('primary_driver',$dict_configs['primary_driver']);
    $tpl->assign('drivers',$dict_drivers);
    $tpl->assign('list_num',$dict_configs['list_num']);
    $tpl->render('admin');
} else {
    if ($dict_login) {
        $page=isset($_GET['page'])?(int)$_GET['page']:1;
        $wl = new WordList('new');
        $tpl->assign('words',$wl->get($dict_configs['list_num'],($page-1)*$dict_configs['list_num']));
        $tpl->assign('page',$page);
        $tpl->assign('num',$dict_configs['list_num']);
        $tpl->assign('count',$wl->count());
    }
    $tpl->assign('lt','new');
    $tpl->render('index');
}
